package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.event.domain.EventChangeType;
import com.ruoyi.event.domain.bo.EventChangeTypeBo;
import com.ruoyi.event.domain.vo.EventChangeTypeVo;
import com.ruoyi.event.mapper.EventChangeTypeMapper;
import com.ruoyi.event.service.IEventChangeTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 人员类型转换Service业务层处理
 *
 * @author xiaohao
 * @date 2023-02-10
 */
@RequiredArgsConstructor
@Service
public class EventChangeTypeServiceImpl implements IEventChangeTypeService {

    private final EventChangeTypeMapper baseMapper;

    /**
     * 查询人员类型转换
     */
    @Override
    public EventChangeTypeVo queryById(Long typeId) {
        return baseMapper.selectVoById(typeId);
    }

    /**
     * 查询人员类型转换列表
     */
    @Override
    public TableDataInfo<EventChangeTypeVo> queryPageList(EventChangeTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventChangeType> lqw = buildQueryWrapper(bo);
        Page<EventChangeTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询人员类型转换列表
     */
    @Override
    public List<EventChangeTypeVo> queryList(EventChangeTypeBo bo) {
        LambdaQueryWrapper<EventChangeType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventChangeType> buildQueryWrapper(EventChangeTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EventChangeType> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getChangeType()), EventChangeType::getChangeType, bo.getChangeType());
        lqw.eq(bo.getUserId() != null, EventChangeType::getUserId, bo.getUserId());
        return lqw;
    }

    /**
     * 新增人员类型转换
     */
    @Override
    public Boolean insertByBo(EventChangeTypeBo bo) {
        EventChangeType add = BeanUtil.toBean(bo, EventChangeType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTypeId(add.getTypeId());
        }
        return flag;
    }

    /**
     * 修改人员类型转换
     */
    @Override
    public Boolean updateByBo(EventChangeTypeBo bo) {
        EventChangeType update = BeanUtil.toBean(bo, EventChangeType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventChangeType entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除人员类型转换
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
